package pl.perseus.test;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.codec.digest.DigestUtils;

import pl.perseus.model.Passlist;
import pl.perseus.model.Rach;
import pl.perseus.model.User;
import pl.perseus.model.Userrach;
import pl.perseus.service.passlist.IPassListService;
import pl.perseus.service.rach.IRachService;
import pl.perseus.service.user.IUserService;
import pl.perseus.service.userrach.IUserRachService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-config.xml")
@Transactional
public class Populator {

	@Autowired
	IUserService userService;
	
	@Autowired
	IRachService rachService;
	
	@Autowired
	IUserRachService userRachService;
	
	@Autowired
	IPassListService passListService;
	
	List<Map<String, Object>> list = new LinkedList<Map<String, Object>>();
	
	@Before
	public void before(){
		for(int i = 1; i<11; i++){
			Map<String , Object> map = new HashMap<String, Object>();
			map.put("usr", "test"+i);
			Random rand = new Random();
			List<Long> listA = new LinkedList<Long>();
			for(int j = 1; j<3; j++){
				long a = (rand.nextInt(9)+1) * 100;
				long b = (rand.nextInt(9)+1) * 100000;
				long c = (rand.nextInt(9)+1) * 10000000;
				listA.add(a+b+c);
			}
			map.put("acc", listA);
			list.add(map);
		}
	}
	
	@Test
	@Rollback(true)
	public void insertUser(){
		for(Map<String, Object> map : list){
			User u = new User();
			u.setLogin(map.get("usr").toString());
			u.setPassword(map.get("usr").toString());
			u.setUsername(map.get("usr").toString());
			u = userService.insertUser(u);
			
			List<Long> listA = (List<Long>) map.get("acc");
			
			for(Long l : listA){
				Rach rach = new Rach();
				rach.setActive(true);
				rach.setAmount((long)(new Random().nextInt(9)+1) * 1000);
				rach.setRach(l);
				rach.setRach_typ("S");
				
				rach = rachService.insertRach(rach);
				
				Userrach ur = new Userrach();
				ur.setId_rach(rach.getId());
				ur.setId_user(u.getId());
				
				userRachService.insertUserRach(ur);
			}
			
			for(int k = 0; k<25; k++){
				Passlist p = new Passlist();
				p.setActive(true);
				p.setId_user(u.getId());
				p.setPasslist((long) k);
				p.setPass(DigestUtils.md5Hex(u.getLogin()));
				passListService.insertPassList(p);
			}
			
		}

	}
	

	
}



